package com.yuer.rpc.netty_base;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.util.CharsetUtil;

public class NettyClientHandler implements ChannelInboundHandler {

    /**
     *  通道就绪事件
     * @param channelHandlerContext
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {
        ChannelFuture channelFuture = channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("你好啊，我是netty客户端", CharsetUtil.UTF_8));
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if(channelFuture.isSuccess()){
                    System.out.println("发送成功");
                }else {
                    System.out.println("发送失败");
                }
            }
        });
    }
    /**
     *  通道的度就绪事件
     * @param channelHandlerContext
     * @param o
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        //Object o 就是 服务端发送过来的一个消息
        ByteBuf byteBuffer = (ByteBuf) o;
        System.out.println("服务端发送过来的消息："+byteBuffer.toString(CharsetUtil.UTF_8));
        System.out.println("服务端发送过来的消息："+byteBuffer.toString());
       // System.out.println(String.format("服务端发送过来的消息：%s",new String(byteBuffer.array(),CharsetUtil.UTF_8)));
    }


    @Override
    public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("执行了方法： channelInactive");
    }

    @Override
    public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("执行了方法： channelRegistered");
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("执行了方法： channelUnregistered");
    }




    @Override
    public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("执行了方法：channelReadComplete ");
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        System.out.println("执行了方法：userEventTriggered ");
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("执行了方法： channelWritabilityChanged");
    }

    @Override
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("执行了方法： handlerAdded");
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {
        System.out.println("执行了方法： handlerRemoved");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable) throws Exception {
        throwable.printStackTrace();
        System.out.println("执行了方法： exceptionCaught： " + throwable.getMessage());
    }
}
